from flask import Blueprint
from flask.views import MethodView
from models import User

user_login_bp = Blueprint('user_login', __name__, url_prefix='/users/login')


class UserLoginApi(MethodView):
    def get(self, username):
        users: User = User.query.filter_by(username=username).all()
        if users:
            results = [
                {
                    'user_username': user.username,
                    'user_password': user.password,
                    'user_role': user.role,
                    'user_cardID': user.cardID
                } for user in users
            ]
            return {
                'status': 'success',
                'message': '用户登录成功',
                'results': results
            }
        else:
            return {
                'status': 'fault',
                'message': '用户登录失败'
            }


user_login_view = UserLoginApi.as_view('user_login_api')